package com.neu.cpsp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.cpsp.dao.EmployeeMapper;
import com.neu.cpsp.dao.TeamGroupMapper;
import com.neu.cpsp.pojo.Employee;
import com.neu.cpsp.pojo.EmployeeEntity;
import com.neu.cpsp.pojo.TeamGroup;
import com.neu.cpsp.pojo.TeamGroupInfo;
import com.neu.cpsp.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author neu.xiaojiacheng
 * @version 1.0
 * @date 2022/2/24 15:00
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private TeamGroupMapper teamGroupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAllInfo(Employee employee) {
        int flag = employeeMapper.insert(employee);
        if (flag > 0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public List<EmployeeEntity> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public List<EmployeeEntity> selectInfoSelective(String department, String teamGroup, Integer isDirectProducer, String name) {
        return employeeMapper.selectInfoSelective(department, teamGroup, isDirectProducer, name);
    }

    @Override
    public Boolean getIsDirectProducer(String idCard) {
        return employeeMapper.getIsDirectProducer(idCard);
    }

    @Override
    public EmployeeEntity selectByCard(String idCard) {
        return employeeMapper.selectByCard(idCard);
    }

    @Override
    public EmployeeEntity selectByJobNumber(String jobNumber) {
        return employeeMapper.selectByJobNumber(jobNumber);
    }

    @Override
    public List<EmployeeEntity> selectEmployeesByTeamGroup(String teamGroup, String department) {
        return employeeMapper.selectEmployeesByTeamGroup(teamGroup, department);
    }

    @Override
    public List<EmployeeEntity> selectEmployeesByDepartment(String department) {
        return employeeMapper.selectEmployeesByDepartment(department);
    }

    @Override
    public List<String> selectCardByDepart(String department) {
        return employeeMapper.selectCardByDepart(department);
    }

    @Override
    public EmployeeEntity getById(Integer id) {
        return employeeMapper.getById(id);
    }

    @Override
    public List<EmployeeEntity> selectByName(String name) {
        return employeeMapper.selectByName(name);
    }


    @Override
    public int updateByPrimaryKeySelective(Employee record) {
        return employeeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public PageInfo<EmployeeEntity> findEmployee(Integer pageNum, Integer pageSize, String departmentId, String teamGroupId, String name){
        PageHelper.startPage(pageNum,pageSize);
        List<EmployeeEntity> employeeList = employeeMapper.findEmployees(departmentId, teamGroupId, name);
        PageInfo<EmployeeEntity> pageInfo = new PageInfo<>(employeeList);
        return pageInfo;
    }


    /**
     * 2022-04-18
     * 修改职工班组和岗位类型
     * @param employeeId 职工id
     * @param teamGroupName 班组名
     * @param positionType 岗位类型
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, readOnly = false, timeout = -1)
    public Integer updateEmployeeTeamAndPosition(Integer employeeId, String teamGroupName, String positionType){

        // 1. 查询员工 获得部门id
        String departmentId = employeeMapper.selectById(employeeId).getDepartmentId();
        // 1.1 未查询到员工，修改失败
        if (departmentId == null)
            return 0;

        // 2. 查询班组是否存在
        TeamGroup teamGroup = teamGroupMapper.selectOne(new LambdaQueryWrapper<TeamGroup>()
                .eq(TeamGroup::getDepartmentId, departmentId)
                .eq(TeamGroup::getTeamGroupName, teamGroupName));
        // 2.1. 没有班组，则创建新的班组
        Integer insert = 0;
        if (null == teamGroup){
            teamGroup = new TeamGroup();
            teamGroup.setTeamGroupName(teamGroupName);
            teamGroup.setDepartmentId(departmentId);
            insert = teamGroupMapper.insert(teamGroup);
        }

        // 3. 更新员工的班组id和岗位类型
        Employee employeeForUpdate = new Employee();
        employeeForUpdate.setId(employeeId);
        employeeForUpdate.setTeamGroupId(teamGroup.getId()+"");        //todo:bug。数据库中team_group表的id为int类型，而employee中team_group_id为varchar类型
        employeeForUpdate.setPositionType(positionType);
        int update = employeeMapper.updateById(employeeForUpdate);
        return insert + update;
    }

    @Override
    public Integer updateEmployeeIs45Days(Integer employeeId, Boolean is45Days) {
        Employee employee = new Employee();
        employee.setId(employeeId);
        employee.setIs45Days(is45Days);
        return employeeMapper.updateById(employee);
    }

    @Override
    public List<TeamGroupInfo> findTeamGroupAndName(String departmentId) {
        List<TeamGroupInfo> teamGroupInfos = teamGroupMapper.findTeamGroupAndName(departmentId);
        return teamGroupInfos;
    }

    @Override
    public Integer updateTeamGroupManagerId(Integer id, String branchManagerId, String mainManagerId) {
        TeamGroup teamGroup = new TeamGroup();
        teamGroup.setId(id);
        teamGroup.setBranchManagerId(branchManagerId);
        teamGroup.setMainManagerId(mainManagerId);
        return teamGroupMapper.updateById(teamGroup);
    }

    @Override
    public Integer addTeamGroup(TeamGroup teamGroup) {
        return teamGroupMapper.insert(teamGroup);
    }

    @Override
    public TeamGroup selectById(Integer id) {
        return teamGroupMapper.selectById(id);
    }


}
